package com.tianchi.lexiang.background;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.util.AVHDMISettingUtil;
public class AVHDMISetting extends Activity {
    /** Called when the activity is first created. */
	private static final String TAG = "AVHDMISwitchActivity";
	
	private String[] screenTypeArr;
	public static String[] realScreenType = {"vga_av","vga_hdmi"};
	//public static String[] realScreenType = {"av","hdmi_720","vga_av","vga_hdmi"};
	public static final String PATH = "/cache/ych";
	private static final String DEFAULT_SCREEN = "vga_av";
	private static final int DEFAULT_POSITION = 2;//  it means vag_av
	
	
	private TextView screenTypePrompt;
	private RadioGroup screenGroup;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avhdmi);
        screenTypeArr = getResources().getStringArray(R.array.screen_type);
        screenTypePrompt = (TextView) findViewById(R.id.screenTypePrompt);
        
        screenGroup = (RadioGroup) findViewById(R.id.screenGroup);
    }
    
    private void initRadioGroup(){
    	int count = screenGroup.getChildCount();
    	for (int i = 0; i < count; i++) {
			((RadioButton)screenGroup.getChildAt(i)).setText(screenTypeArr[i]);
		}
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	initRadioGroup();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	Log.d(TAG, "[onResume]");
    	((RadioButton)screenGroup.getChildAt(getCurrentScreenTypePosition())).setChecked(true);
    	showCurrentScreenType();
    }
    
    private int getCurrentScreenTypePosition(){
    	int position = -1;
    	String type = getCurrentScreenTypeName();
    	Log.d(TAG, "[getCurrentScreenTypePosition] type ==>" + type);
    	for (int i = 0; i < realScreenType.length; i++) {
			if (type.equals(realScreenType[i])) {
				position = i;
			}
		}
    	Log.d(TAG, "[getCurrentScreenTypePosition] position=>" + position);
    	return position;
    }
    
    private void createNewFile(String fileName){
    	AVHDMISettingUtil.createNewFile(fileName);
    }
    
    private void deleteOldFile(){
    	AVHDMISettingUtil.deleteOldFile();
    }
    
    private File checkDir(){
    	return AVHDMISettingUtil.checkDir();
    }
    
    private void showCurrentScreenType(){
    	int postion = getCurrentScreenTypePosition();
    	Log.d(TAG, "[showCurrentScreenType]postion == >" + postion);
    	String fakeShowName = screenTypeArr[postion];
    	screenTypePrompt.setText(getString(R.string.current_screen_type)+fakeShowName);
    }
    
    private String getCurrentScreenTypeName(){
    	String name = "";
    	String[] files = checkDir().list();
    	if (files.length == 1) {
			name = files[0];
			Log.d(TAG, "the current file name = " + name);
		}else if (files.length == 0) {
			Log.d(TAG, "[showCurrentScreenType] this is first time to set screent type");
			createNewFile(DEFAULT_SCREEN);
			name = DEFAULT_SCREEN;
		}else if(files.length > 1){
			Log.d(TAG, "[showCurrentScreenType] "+ PATH + " has file number " + files.length);
			deleteOldFile();
			createNewFile(DEFAULT_SCREEN);
			name = DEFAULT_SCREEN;
		}
    	return name;
    }
    
    public void backClick(View v){
    	finish();
    }
    
    public void confirmClick(View v){
    	promptRebootDialog();
    }
    
    private int getCurrentSelectPostion(){
    	int count = screenGroup.getChildCount();
    	for (int i = 0; i < count; i++) {
			if(((RadioButton)screenGroup.getChildAt(i)).isChecked()){
				return i;
			}
		}
    	return DEFAULT_POSITION;
    }
    
    private void promptRebootDialog(){
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(R.string.reboot_prompt_title);
    	builder.setMessage(R.string.reboot_prompt_message);
    	builder.setPositiveButton(R.string.confirm_reboot, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				deleteOldFile();
				int postion = getCurrentSelectPostion();
				Log.d(TAG, "[promptRebootDialog] current select postion is =" + postion);
				String screen = realScreenType[postion];
				createNewFile(screen);
				showCurrentScreenType();
				reboot();
			}
		});
    	
    	builder.setNegativeButton(R.string.just_think, new DialogInterface.OnClickListener() {
    		@Override
    		public void onClick(DialogInterface dialog, int which) {
    			dialog.dismiss();
    		}
    	});
    	
    	builder.create();
    	builder.show();
    }
    
    private void reboot(){
    	Log.d(TAG, "will reboot the device");
    	Intent i = new Intent(Intent.ACTION_REBOOT);   
    	i.putExtra("nowait", 1);   
    	i.putExtra("interval", 1);   
    	i.putExtra("window", 0);   
    	sendBroadcast(i);
    }
    
}
